package net.ufrog.common.spring.exception;

import net.ufrog.common.Result;
import net.ufrog.common.exception.ServiceException;
import net.ufrog.common.utils.Strings;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.View;

/**
 * @author ultrafrog, ufrog.net@gmail.com
 * @version 0.1, 2017-02-04
 * @since 0.1
 */
public class ServiceExceptionHandler implements ExceptionHandler<ServiceException> {

    @Override
    public Boolean isType(Exception e) {
        return e.getClass() == ServiceException.class;
    }

    @Override
    public ModelAndView handle(ServiceException e, String requestType, String errorView, String partViewSuffix, View jsonView, ExceptionLogger exceptionLogger) {
        exceptionLogger.log(e, e.getCode(), e.getMessage(), e.getLocalizedMessage());
        return ExceptionResolver.modelAndView(requestType, errorView, partViewSuffix, jsonView, ExceptionResolver.KEY_MODEL_EXCEPTION, Result.failure(e, e.getLocalizedMessage(), e.getCode()));
    }
}
